﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using WebServer.DBAccess;
using WebServer.Models;

namespace WebServer.Biz
{
    public class BourseBiz
    {
        #region 交易所信息-总数/列表[字段少/可根据名称查询/字段多]/热门交易所/根据CenterID获取信息/修改/删除/交易所最大CenterID/交易所统计
        /// <summary>
        /// 交易所总数
        /// </summary>
        /// <param name="Simple"></param>
        /// <param name="IsOpen"></param>
        /// <returns></returns>
        public int BourseCounts(int IsOpen)
        {
            int result = 0;
            result = new BourseDA().BourseCounts(IsOpen);
            return result;
        }


        /// <summary>
        /// 获取交易所列表【字段少】
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="PageSize"></param>
        /// <param name="isOpen"></param>
        /// <param name="OrderID">0-创建时,1-Name,2-Tel，3-Email，4-Address，5-Img</param>
        /// <param name="Desc"></param>
        /// <returns></returns>
        public List<BourseA> BourseListA(int Page, int PageSize, bool isOpen, int Order, bool Desc)
        {
            List<BourseA> request = new List<BourseA>();
            request = new BourseDA().BourseListA(Page, PageSize, isOpen, Order, Desc);
            return request;
        }

        /// <summary>
        /// 获取交易所列表【可根据名称查询】
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="PageSize"></param>       
        /// <param name="isOpen"></param>
        /// <param name="Remak">查询条件【名称】</param>
        /// <param name="OrderID">0-创建时,1-Name,2-Tel，3-Email，4-Address，5-Img</param>
        /// <returns></returns>
        public List<BourseC> BourseListB(int Page, int PageSize, int isOpen, string Remak, int Order, bool Desc)
        {
            List<BourseC> request = new List<BourseC>();
            request = new BourseDA().BourseListB(Page, PageSize, isOpen, Remak, Order, Desc);
            return request;
        }



        /// <summary>
        /// 获取交易所列表【字段多】
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="PageSize"></param>       
        /// <param name="isOpen"></param>
        /// <param name="OrderID">0-创建时,1-Name,2-Tel，3-Email，4-Address，5-Img</param>
        /// <returns></returns>
        public List<BourseC> BourseListC(int Page, int PageSize, int isOpen, int Order, bool Desc)
        {
            List<BourseC> request = new List<BourseC>();
            request = new BourseDA().BourseListC(Page, PageSize, isOpen, Order, Desc);
            return request;
        }

        /// <summary>
        /// 热门交易所
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Sort">默认根据回复，1-创建时间</param>
        /// <param name="Desc"></param>
        /// <returns></returns>
        public List<BourseA> BourseSort(int PageSize, int PageIndex, int Sort, bool Desc)
        {
            List<BourseA> request = new List<BourseA>();
            request = new BourseDA().BourseSort(PageSize, PageIndex, Sort, Desc);
            return request;
        }


        /// <summary>
        /// 查询指定交易所
        /// </summary>
        /// <param name="CenterID"></param>
        /// <returns></returns>
        public BourseC BourseByCenterID(int CenterID)
        {
            BourseC request = new BourseC();
            request = new BourseDA().BourseByCenterID(CenterID);
            return request;
        }

        /// <summary>
        /// 修改或者添加交易所
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public MsgEntity BourseSet(BourseC entity)
        {
            MsgEntity M = new MsgEntity();

            int result = 0;
            result = new BourseDA().BourseSet(entity);
            if (result > 0)
            {
                M.Code = 10000;
                M.Msg = result.ToString();
            }          
            else
            {
                M.Code = 20000;
                M.Msg = "操作出错";
            }
            return M;
        }

        /// <summary>
        /// 删除交易所
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public MsgEntity BourseDel(int ID)
        {
            MsgEntity M = new MsgEntity();

            int result = 0;
            result = new BourseDA().BourseDel(ID);
            if (result > 0)
            {
                M.Code = 10000;
                M.Msg = result.ToString();
            }
            else if (result == -1)
            {
                M.Code = 20000;
                M.Msg = "错误";
            }
            else
            {
                M.Code = 30000;
                M.Msg = "交易所已开放";
            }
            return M;
        }
        
        /// <summary>
        /// 交易所最大CenterID
        /// </summary>
        /// <returns></returns>
        public int getMaxCenterID()
        {
            int result = 0;
            result = new BourseDA().getMaxCenterID();
            return result;
        }


        /// <summary>
        /// 交易所统计
        /// </summary>
        /// <returns></returns>
        public string BourseCount()
        {
            string request = "0,0,0";
            ObjectCache cache = MemoryCache.Default;
            request = cache["BourseCount"] as string;
            if (string.IsNullOrEmpty(request))
            {
                request = "";
                int Bcount = BourseCounts(0);
                int Ocount = BourseCounts(1);
                int Gcount = GoodsCount(0, "", "", -1);
                request = Bcount + "," + Ocount + "," + Gcount;
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTime.Parse(DateTime.Now.AddDays(1).Year + "-" + DateTime.Now.AddDays(1).Month + "-" + DateTime.Now.AddDays(1).Day + " 00:00:00");
                cache.Set("BourseCount", request, policy);
            }

            return request;
        }
        #endregion


        #region 藏品信息-总数/列表[可根据名称查询]/根据CenterID，Code获取信息/获取最近N天上线的/藏品介绍/根据交易所CenterID获取藏品库存/修改/删除
        /// <summary>
        /// 藏品总数
        /// </summary>
        /// <param name="Simple"></param>
        /// <param name="IsOpen"></param>
        /// <returns></returns>
        public int GoodsCount(int CenterID, string Code, string Key, int IsOpen)
        {
            int result = 0;
            result = new BourseDA().GoodsCount(CenterID, Code, Key, IsOpen);
            return result;
        }


        /// <summary>
        /// 获取藏品列表【根据名称查询】
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="PageSize"></param>
        /// <param name="CenterID"></param>
        /// <param name="Code"></param>
        /// <param name="Simple"></param>
        /// <param name="Key"></param>
        /// <param name="IsOpen"></param>
        /// <param name="Order">默认-修改时间,1-CenterID,2-Shortened,3-Code,4-Fullname,5-OnLinePrice,6-OnLinePrice,7-Stock,8-Circulation,9-PublishTime</param>
        /// <param name="Desc"></param>
        /// <returns></returns>
        /// <returns></returns>
        public List<Goods> GoodsListA(int Page, int PageSize, int CenterID, string Code, bool Simple, string Key, int IsOpen, int Order, bool Desc)
        {
            List<Goods> request = new List<Goods>();
            request = new BourseDA().GoodsListA(Page, PageSize, CenterID, Code, Simple, Key, IsOpen, Order, Desc);
            return request;
        }


        /// <summary>
        /// 查询指定藏品
        /// </summary>
        /// <param name="CenterID"></param>
        /// <param name="Code"></param>
        /// <returns></returns>s
        public Goods GoodsByCode(int CenterID, string Code)
        {
            Goods request = new Goods();
            request = new BourseDA().GoodsByCode(CenterID, Code);
            return request;
        }

        /// <summary>
        /// 获取最近N天上线的
        /// </summary>
        /// <param name="Times"></param>
        /// <param name="Cached">更新缓存</param>
        /// <returns></returns>
        public List<Goods> GoodsListed(int Days, bool Cached)
        {
            List<Goods> request = new List<Goods>();
            ObjectCache cache = MemoryCache.Default;
            CacheItemPolicy policyRB = new CacheItemPolicy();
            policyRB.AbsoluteExpiration = DateTime.Now.AddHours(6);

            if (Cached == true)
            {
                request = new List<Goods>();
                request = new BourseDA().GoodsListed(DateTime.Now.AddDays(Days * -1));                
                cache.Set("GoodsListed", request, policyRB);
            }
            else
            {
                request = cache["GoodsListed"] as List<Goods>;
                if (request == null)
                {
                    request = new List<Goods>();
                    request = new BourseDA().GoodsListed(DateTime.Now.AddDays(Days * -1));                   
                    cache.Set("GoodsListed", request, policyRB);
                }
            }
            return request;
        }


        /// <summary>
        /// 藏品介绍
        /// </summary>
        /// <param name="CenterID"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public string GoodsIntroduce(int CenterID, string Code)
        {
            string request = string.Empty;
            request = new BourseDA().GoodsIntroduce(CenterID, Code);
            return request;
        }

        /// <summary>
        /// 根据交易所CenterID获取藏品库存
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="PageSize"></param>
        /// <param name="CenterID"></param>
        /// <param name="Code"></param>
        /// <param name="Simple"></param>
        /// <param name="Key"></param>
        /// <param name="IsOpen"></param>
        /// <param name="Order">默认-修改时间,1-CenterID,2-Shortened,3-Code,4-Fullname,5-OnLinePrice,6-OnLinePrice,7-Stock,8-Circulation,9-PublishTime</param>
        /// <param name="Desc"></param>
        /// <returns></returns>
        public List<GoodsStock> GoodsStock(int Page, int PageSize, int CenterID, string Code, bool Simple, string Key, int IsOpen, int Order, bool Desc)
        {
            List<GoodsStock> request = new List<GoodsStock>();
            request = new BourseDA().GoodsStock(Page, PageSize, CenterID, Code, Key, IsOpen, Order, Desc);
            return request;
        }

        /// <summary>
        /// 修改或者添加藏品
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public MsgEntity GoodsSet(Goods entity)
        {
            MsgEntity M = new MsgEntity();

            int result = 0;
            result = new BourseDA().GoodsSet(entity);
            if (result > 0)
            {
                M.Code = 10000;
                M.Msg = result.ToString();
            }
            else
            {
                M.Code = 20000;
                M.Msg = "操作出错";
            }
            return M;
        }

        /// <summary>
        /// 删除藏品
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public MsgEntity GoodsDel(int ID)
        {
            MsgEntity M = new MsgEntity();

            int result = 0;
            result = new BourseDA().GoodsDel(ID);
            if (result > 0)
            {
                M.Code = 10000;
                M.Msg = result.ToString();
            }
            else
            {
                M.Code = 20000;
                M.Msg = "删除出错";
            }
            return M;
        }

        #endregion



        #region App首页显示交易所-App首页显示交易所列表/ 是否显示在APP首页
        /// <summary>
        /// App首页显示交易所列表
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="PageSize"></param>
        /// <param name="isOpen"></param>
        /// <param name="Remak"></param>
        /// <param name="OrderID">0-创建时,1-Name,2-Tel，3-Email，4-Address，5-Img</param>
        /// <param name="Desc"></param>
        /// <param name="Simple"></param>
        /// <returns></returns>
        public List<BourseC> BourseAPPQ(int Page, int PageSize, int isOpen, string Remak, int OrderID, bool Desc, bool Simple)
        {
            List<BourseC> request = new List<BourseC>();
            request = new BourseDA().BourseAPPQ(Page, PageSize, isOpen, Remak, OrderID, Desc, Simple);
            return request;
        }

        /// <summary>
        /// 是否显示在APP首页
        /// </summary>
        /// <param name="CenterID"></param>
        /// <param name="IsShow">true-显示，false-取消显示</param>
        /// <returns></returns>
        public MsgEntity BourseAPP(int CenterID, bool IsShow)
        {
            MsgEntity M = new MsgEntity();

            int result = 0;
            result = new BourseDA().BourseAPP(CenterID, IsShow);
            if (result > 0)
            {
                M.Code = 10000;
                M.Msg = result.ToString();
            }
            else if (result == -1)
            {
                M.Code = 30000;
                M.Msg = "已存在";
            }
            else if (result == -2)
            {
                M.Code = 40000;
                M.Msg = "找不到";
            }
            else
            {
                M.Code = 20000;
                M.Msg = "错误";
            }
            return M;
        }
        #endregion


        #region PC首页显示交易所-PC首页显示交易所/修改
        /// <summary>
        ///  PC首页显示交易所
        /// </summary>
        /// <returns></returns>
        public BoursePC BoursePCList()
        {
            BoursePC entity = new BoursePC();
            entity = new BourseDA().BoursePCList();
            return entity;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Fn"></param>
        /// <param name="PCValue"></param>
        /// <returns></returns>
        public MsgEntity BoursePCSet(int Fn, string PCValue)
        {
            MsgEntity M = new MsgEntity();

            int result = 0;
            result = new BourseDA().BoursePCSet(Fn, PCValue);
            if (result > 0)
            {
                M.Code = 10000;
                M.Msg = result.ToString();
            }
            else if (result == 0)
            {
                M.Code = 30000;
                M.Msg = "请检查输入的信息";
            }           
            else
            {
                M.Code = 20000;
                M.Msg = "错误";
            }
            return M;
        }
        #endregion
        ///// <summary>
        ///// 去除晚盘，更改白盘
        ///// </summary>
        ///// <param name="Page"></param>
        ///// <param name="PageSize"></param>
        ///// <param name="Simple"></param>
        ///// <param name="isOpen"></param>
        ///// <returns></returns>
        //public List<BourseC> BourseListRemark(int Page, int PageSize, bool Simple, int isOpen)
        //{
        //    List<BourseC> request = new List<BourseC>();
        //    request = new BourseDA().BourseListRemark(Page, PageSize, Simple, isOpen);
        //    for (int i = 0; i < request.Count(); i++)
        //    {
        //        if (request[i].Name.Contains("白盘"))
        //        {
        //            request[i].Name = request[i].Name.Replace("白盘", "文交所");
        //            request[i].Shortened = request[i].Shortened.Replace("白", "");
        //        }
        //        if (request[i].Name.Contains("晚盘"))
        //        {
        //            request.Remove(request[i]);
        //        }
        //    }
        //    return request;
        //}
              
    }
}
